package zengqiang;

import javax.servlet.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

public class HelloServlet implements Servlet {
    public HelloServlet() {
        System.out.println("初始化");
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        //获取初始化参数
        Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
        String username = servletConfig.getInitParameter("username");
        //获取serlvet的名字
        String servletName = servletConfig.getServletName();
        System.out.println("init"+username);
        //获取serlvet的上下文，其实就是application对象
        ServletContext servletContext = servletConfig.getServletContext();
        //获取应用初始化对象
        String mysql = servletContext.getInitParameter("mysql");
        Enumeration<String> initParameterNames1 = servletContext.getInitParameterNames();
        //获取文件在服务器上的绝对路径（此时是获取根目录下面的ServletSpace.iml）
        String path=servletContext.getRealPath("/ServletSpace.iml");
        System.out.println(path);
        //获取当前web应用的名称
        String contextPath = servletContext.getContextPath();
        System.out.println(contextPath);
        //获取当前web应用的某一个文件对应的输入流
        //此处的路径是部署好的之后的路径
        InputStream resourceAsStream = servletContext.getResourceAsStream("/WEB-INF/node.txt");
        System.out.println("地址:"+resourceAsStream);
    }

    @Override
    public ServletConfig getServletConfig() {
        System.out.println("getServletConfig");
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("service");
    }

    @Override
    public String getServletInfo() {
        System.out.println("getServletInfo");
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("destroy");
    }
}
